var express = require('express');
var router = express.Router();
var User = require('../models/users')

/* GET users listing. */
router.get('/', function(req, res, next) {
  res.send('respond with a resource');
});

// 登录
router.post('/login', function (req, res, next) {
  var param = {
    userName: req.body.userName,
    userPwd: req.body.userPwd
  }
  User.findOne(param, function (err, doc) {
    if (err) {
      res.json({
        status: 1,
        msg: err.message
      })
    } else {
      if (doc) {
        res.cookie('userId', doc.userId, {
          path: '/',
          maxAge: 1000 * 60 * 60
        })
        res.cookie('userName', doc.userName, {
          path: '/',
          maxAge: 1000 * 60 * 60
        })
        res.json({
          status: 0,
          msg: '',
          result: {
            userName: doc.userName
          }
        })
      }
    }
  })
})

// 退出登录
router.post('/logout', function (req, res, next) {
  res.cookie('userId', '', {
    path: '/',
    maxAge: -1
  })
  res.json({
    status: 0,
    msg: '',
    result: ''
  })
})

// 登录校验
router.get('/checkLogin', function (req, res, next) {
  if (req.cookies.userId) {
    res.json({
      status: 0,
      msg: '',
      result: req.cookies.userName || ''
    })
  } else {
    res.json({
      status: 1,
      msg: '未登录',
      result: ''
    })
  }
})

// 获取购物车列表
router.get('/cartList', function (req, res, next) {
  var userId = req.cookies.userId
  User.findOne({ userId: userId }, function (err, doc) {
    if (err) {
      res.json({
        status: 1,
        msg: err.message,
        result: ''
      })
    } else {
      if (doc) {
        res.json({
          status: 0,
          msg: '',
          result: doc.cartList
        })
      }
    }
  })
})

// 删除购物车
router.post('/deleteCart', function (req, res, next) {
  var userId = req.cookies.userId
  var productId = req.body.productId
  User.update({
    userId: userId
  }, {
    $pull: {
      'cartList': {
        'productId': productId
      }
    }
  }, function (err, doc) {
    if (err) {
      res.json({
        status: 0,
        msg: err.message,
        result: ''
      })
    } else {
      res.json({
        status: 0,
        msg: '',
        result: 'suc'
      })
    }
  })
})

router.post('/editCart', function (req, res, next) {
  let userId = req.cookies.userId
  let productId = req.body.productId
  let productNum = req.body.productNum
  let checked = req.body.checked
  User.update({'userId': userId, 'cartList.productId': productId}, {
    'cartList.$.productNum': productNum,
    'cartList.$.checked': checked
  }, function (err, doc) {
    if (err) {
      res.json({
        status: '1',
        msg: err.message,
        result: ''
      })
    } else {
      res.json({
        status: '0',
        msg: '',
        result: 'suc'
      })
    }
  })
})

router.post('/register', function (req, res, next) {
  var userId = parseInt(Math.random() * 10000000000)
  var userName = req.body.name
  var userPwd = req.body.pwd
  User.findOne({ userName: userName }, function (err, userDoc) {
    if (err) {
      res.json({
        status: 1,
        msg: err.message
      })
    } else {
      if (userDoc) {
        res.json({
          status: 1002,
          msg: '该用户已注册'
        })
      } else {
        User.create({
          userId: userId,
          userName: userName,
          userPwd: userPwd
        }, function (err, doc) {
          if (err) {
              res.json({
                  status: 1,
                  msg: err.message,
                  result: ''
              })
          } else {
              res.json({
                  status: 0,
                  msg: '注册成功',
                  result: userId
              })
          }
        })
      }
    }
  })
})

module.exports = router;
